<?php
class Estados_M extends I_Model {
	protected $_table_name = 'estados';
	protected $_primary_key = 'ID';
	protected $_primary_filter = 'intval';
	protected $_order_by = 'NOME';
	public $rules = array ();
	protected $_timestamps = FALSE;
	public function __construct() {
		parent::__construct ();
	}
	function get_count_lotes($id= null) {
		$this->db->select ( 'uf.UF,uf.NOME,uf.ID,COUNT(DISTINCT lt.ID) AS TOTAL' )->from ( 'lotes as lt' )
		->join ( 'cidades as cid', 'lt.CIDADE=cid.ID' )
		->join ( 'estados as uf', 'cid.ESTADO=uf.ID' )
		->join ( 'leiloes as le', 'lt.LEILAO_ID=le.ID' )
		->where( '(le.STATUS = 2  OR le.STATUS = 1)' );
		if (! is_null ( $id )) {
			$this->db->where ( 'le.ID', $id );
		}
		$this->db->group_by ( 'uf.UF', 'ASC' );
		
		return $this->db->get ()->result ();
	}
        
        function get_count_lotes_closed($id= null) {
		$this->db->select ( 'uf.UF,uf.NOME,uf.ID,COUNT(DISTINCT lt.ID) AS TOTAL' )->from ( 'lotes as lt' )
		->join ( 'cidades as cid', 'lt.CIDADE=cid.ID' )
		->join ( 'estados as uf', 'cid.ESTADO=uf.ID' )
		->join ( 'leiloes as le', 'lt.LEILAO_ID=le.ID' )
		->where( '(le.STATUS = 4)' );
		if (! is_null ( $id )) {
			$this->db->where ( 'le.ID', $id );
		}
		$this->db->group_by ( 'uf.UF', 'ASC' );
		
		return $this->db->get ()->result ();
	}
	
	
	function get_count_lotes_by_slug($slug) {
		$this->db->select ( 'uf.UF,uf.NOME,uf.ID,COUNT(DISTINCT lt.ID) AS TOTAL' )->from ( 'lotes as lt' )
		->join ( 'cidades as cid', 'lt.CIDADE=cid.ID' )
		->join ( 'estados as uf', 'cid.ESTADO=uf.ID' )
		->join ( 'leiloes as le', 'lt.LEILAO_ID=le.ID' )
		->join('tipos_ativos as tp','tp.ID=lt.TIPO_ID')
		->join('categorias as cat','cat.ID=tp.CATEGORIA_ID')
		->where( '(le.STATUS = 2  OR le.STATUS = 1)' );
		if (! is_null ( $slug )) {
			$this->db->where ( 'cat.SLUG', $slug );
		}
		$this->db->group_by ( 'uf.UF', 'ASC' );
	     $q = $this->db->get ();
		 return $q->result ();
	}


	function get_count_search($search) {
		$this->db->select ( 'uf.UF,uf.NOME,uf.ID,COUNT(DISTINCT lt.ID) AS TOTAL' )->from ( 'lotes as lt' )
		->join ( 'cidades as cid', 'lt.CIDADE=cid.ID' )
		->join ( 'estados as uf', 'cid.ESTADO=uf.ID' )
		->join ( 'leiloes as le', 'lt.LEILAO_ID=le.ID' )
		->where( '(le.STATUS = 2  OR le.STATUS = 1)' )
		->like('lt.DESCRICAO',$search)
		->or_like('lt.TAGS',$search)
		->or_like('lt.SUB_DESCRICAO',$search)
		->or_like('lt.DESCRICAO_DETALHADA',$search);


		$this->db->group_by ( 'uf.UF', 'ASC' );
		
		return $this->db->get ()->result ();
	}
}
